const users: any[] = [];
for (let i = 0; i < 50; i++) {
  users.push({
    id: i.toString(),
    name: 'name' + i,
    age: Math.round(Math.random() * 100),
    data: {
      title: Math.round(Math.random() * 100),
    },
    birthday: new Date(),
    deleted: false
  });
}

export default {
  'GET /api/users/one': (req: any, res: any) => {
    const id = req.query.id;
    const index = users.findIndex((user) => user.id === id);
    res.end(JSON.stringify(users[index]));
  },
  'GET /api/users': (req: any, res: any) => {
    const queryParameters = req.query;
    const indexStart = queryParameters.pageIndex * queryParameters.pageSize;
    let indexEnd = indexStart + queryParameters.pageSize * 1;
    if (indexEnd > users.length) {
      indexEnd = users.length;
    }
    const data: any = {
      totalElements: users.length,
    };

    if (queryParameters.id) {
      data.content = users
        .filter((user) => user.id === queryParameters.id)
        .splice(indexStart, indexEnd);
    } else {
      data.content = users.slice(indexStart, indexEnd);
    }

    res.end(JSON.stringify(data));
  },
  'POST /api/users': (req: any, res: any) => {
    users.push(req.body);
    res.end();
  },
  'PUT /api/users': (req: any, res: any) => {
    const index = users.findIndex((user) => user.id === req.body.id);
    users[index] = req.body;
    res.end();
  },
  'DELETE /api/users': (req: any, res: any) => {
    res.status(500).end();
    return;
    req.body.forEach((id: string) => {
      const index = users.findIndex((user) => user.id === id);
      if (index > -1) {
        users.splice(index, 1);
      }
    });
    res.end();
  },
};
